class ClassesMgrController {
    def createBlogService
    def index = { }

    def addCourseTeacher = {
        def lst = org.grails.plugins.lookups.Lookup.codeList("courses")
        Classes classes = Classes.get(params.id)
        log.info(classes.teacherCourseClasses)
        [coursesList:lst,classesInstance:classes,teacherCourse:classes.teacherCourseClasses]
    }

    def courseTeacherSave = {
        Classes c = Classes.get(params.classesid)
        def crouseTeacherIDList = params.selectedTeacher.tokenize('|')
        
        TeacherCourseClasses.findAllByClasses(c).each{
            it.delete(flush:true)
        }

        crouseTeacherIDList.each{
            def aCrouseTeacher = it.tokenize(',')
            log.info("课程："+aCrouseTeacher[0])
            log.info("教师ID："+aCrouseTeacher[1])
            if(aCrouseTeacher[0] && aCrouseTeacher[1]){
                TeacherCourseClasses teachercc = new TeacherCourseClasses()
                teachercc.course = aCrouseTeacher[0].trim()
                teachercc.teacher = Teacher.get(aCrouseTeacher[1].toLong())
                teachercc.classes = c
                teachercc.save()
            }
        }
        log.info(crouseTeacherIDList)
        flash.message = "添加代课老师成功！"
        redirect(action:"addCourseTeacher", id:params.classesid)
    }

    // todo可能一次生成的班级下的代课老师的列表不正确，这个方法可以调用，生成该班级的教师
     def  saveTeacherCourseClasses = {
        def classes = Classes.get(params.classesid)
        if(!classes){
            flash.message = "此班级没有添加任何的课表，也就没有了代课老师，请添加课表，课表生成后即可自动生成代课老师，如果有错再点击此按钮！"
            return redirect(controller: "classes", action: "list")
        }
        TeacherCourseClasses.findAllByClasses(classes).each{
            it.delete(flush:true)
        }
        def teacherList = Plan.findAllByClasses(classes).grep{
            it.teacher
        }
        if(teacherList.size() > 0){
            teacherList.each{
                TeacherCourseClasses teachercc = new TeacherCourseClasses()
                teachercc.course = it.course
                teachercc.teacher = it.teacher
                teachercc.classes = it.classes
                teachercc.save()
            }
        }
       
        return redirect(controller: "classes", action: "list")
    }
 

    def createblog = {
          def classesInstance 
          if (params.id) {
            classesInstance = Classes.get(params.id)
        }else{
             flash.message = "没有找到此班级！"
             return redirect(controller: "classes", action: "list")
        }
         if(createBlogService.sendCreateClassesBlog(classesInstance,grailsApplication.config.grails.default.blog.create.uri)){
                flash.message = "创建班级博客成功！"
         }else{
                flash.message = "创建班级博客失败，请重试或联系管理员！"
         }
         return redirect(controller: "classes", action: "list")
     }

     // 开启和停用班级
    def goswitch = {
        if (params.id) {
            def classes = Classes.get(params.id)
            classes.enable = !classes.enable
            classes.save(flush: true)
        }
        redirect(action: "list" , controller: "classes")
    }

     // 家长对班级的提问
    def comments = {
        
    }

         // 家长对班级的提问
    def commentShow = {
        
    }

}
